package com.jcl.recruit.corp.service;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.corp.logic.SubcompanyLogic;
import com.jcl.recruit.corp.model.Subcompany;
import com.jcl.recruit.corp.vo.CorpVo;
import com.jcl.recruit.user.logic.UserExternalAccessLogic;
import com.jcl.recruit.user.logic.UserLogic;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.model.UserExternalAccess;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/2 下午1:38
 */
@Service
public class SubcompanyService {

    @Autowired
    private SubcompanyLogic subcompanyLogic;

    @Autowired
    private UserLogic userLogic;

    @Autowired
    private UserExternalAccessLogic userExternalAccessLogic;

    public List<Subcompany> selectAllComanyInfosBySuperId(Integer superId) {
        return subcompanyLogic.selectAllComanyInfosBySuperId(superId);
    }

    public Subcompany findOneByCode(String code) {
        return subcompanyLogic.findOneByCode(code);
    }

    public CorpVo getCorpDetail() {
        String corpCode = RequestUtils.getCorpCode();
        Subcompany subcompany = subcompanyLogic.findOneByCode(corpCode);
        CorpVo corpVo = new CorpVo();
        BeanUtils.copyProperties(subcompany, corpVo);
        if(null != subcompany.getAddUser()){
            User user = userLogic.findOneById(subcompany.getAddUser());
            if(null != user){
                corpVo.setAddUserName(user.getRealname());
                corpVo.setAddUserPostName(user.getPostName());
            }
        }
        return corpVo;
    }

    public List<Map<String, Object>> userLoginCorpList(String category){

        List<Map<String, Object>> list= new ArrayList<>();
        String userId = RequestUtils.getUserId();
        UserExternalAccess access = userExternalAccessLogic.findOneByUserId(Integer.valueOf(userId), category);
        if(null != access){
            List<UserExternalAccess> accessList = userExternalAccessLogic.findListByExternalKey(
                    access.getExternalKey(), UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT, null);

            if(CollectionUtils.isNotEmpty(accessList)){
                List<String> corpCodes = new ArrayList<>();
                for(UserExternalAccess access1: accessList){
                    if(access1.getStatus() == UserExternalLoginConstant.USER_ACCESS_STATUS_USED
                            || access1.getStatus() == UserExternalLoginConstant.USER_ACCESS_STATUS_UNUSED){
                        corpCodes.add(access1.getCorpCode());
                    }
                }
                Map<String, String> corpNameMap = new HashMap<>();
                if(CollectionUtils.isNotEmpty(corpCodes)){
                    corpNameMap = subcompanyLogic.getCorpNameMap(corpCodes);
                }
                for(UserExternalAccess access1: accessList){
                    if(access1.getStatus() == UserExternalLoginConstant.USER_ACCESS_STATUS_USED
                            || access1.getStatus() == UserExternalLoginConstant.USER_ACCESS_STATUS_UNUSED){

                        Map<String, Object> temp = new HashMap<>();
                        temp.put("username",access1.getUsername());
                        temp.put("corpName",corpNameMap.get(access1.getCorpCode()));
                        temp.put("active", false);
                        if(access1.getStatus() == UserExternalLoginConstant.USER_ACCESS_STATUS_USED){
                            temp.put("active",true);
                        }
                        list.add(temp);
                    }
                }
            }
        }

        return list;
    }

}
